package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.GlideRoundTransform;
import com.egg.ylt.pojo.MemberBabyEntity;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sichaoqi on 2018/10/9.
 */

public class ADA_BabyManageRecycler extends RecyclerView.Adapter<ADA_BabyManageRecycler.BabyViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<MemberBabyEntity.ListBean> mList;
    private OnItemClickListener itemClickListener;
    private OnDeleteClickListener deleteClickListener;
    private OnDefaultClickListener defaultClickListener;

    private String lastId;
    private AppSharedPreferences mSp;

    public ADA_BabyManageRecycler(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
        mSp = new AppSharedPreferences(mContext);
    }

    //添加数据
    public void addList(List<MemberBabyEntity.ListBean> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public BabyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BabyViewHolder(mInflater.inflate(R.layout.item_baby_recycler, parent, false));
    }

    @Override
    public void onBindViewHolder(BabyViewHolder holder, int position) {
        final MemberBabyEntity.ListBean entity = mList.get(position);


        //头像
        Glide.with(mContext)
                .load(entity.getImageUrl())
                .transform(new GlideRoundTransform(mContext))
                .transition(new DrawableTransitionOptions().crossFade())
                .into(holder.itemBabyIvPhoto);
        //昵称
        holder.itemBabyIvName.setText(entity.getName());
        //性别 0男  1女
        if ("0".equals(entity.getGender())){
            holder.itemBabyIvSex.setImageResource(R.mipmap.ic_baby_boy);
        }else if ("1".equals(entity.getGender())){
            holder.itemBabyIvSex.setImageResource(R.mipmap.ic_baby_girl);
        }
        //生日
        holder.itemBabyTvBirthday.setText(entity.getBirthDay());
        //身高
        holder.itemBabyTvHeight.setText("身高" + entity.getHeight() + "cm");
        //体重
        holder.itemBabyTvWeight.setText("体重" + entity.getWeight() + "kg");
        //状态
        switch (entity.getStatus()){
            case "0":
                holder.itemBabyTvDefault.setVisibility(View.VISIBLE);
                holder.itemBabyIvDelete.setVisibility(View.VISIBLE);
                break;
            case "1":
                lastId = entity.getId();
                //把默认宝宝的id存入本地
                mSp.putString(Constants.BABY_ID, entity.getId());
                holder.itemBabyTvDefault.setVisibility(View.GONE);
                holder.itemBabyIvDelete.setVisibility(View.GONE);
                break;
        }

        //删除按钮的监听
        holder.itemBabyIvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteClickListener.onDeleteClick(entity.getId());
            }
        });

        //条目的监听
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onClick(entity);
            }
        });

        //设置默认
        holder.itemBabyTvDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                defaultClickListener.onDefaultClick(lastId, entity.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class BabyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_baby_iv_photo)
        ImageView itemBabyIvPhoto;
        @BindView(R.id.item_baby_iv_sex)
        ImageView itemBabyIvSex;
        @BindView(R.id.item_baby_iv_name)
        TextView itemBabyIvName;
        @BindView(R.id.item_baby_tv_birthday_left)
        TextView itemBabyTvBirthdayLeft;
        @BindView(R.id.item_baby_tv_birthday)
        TextView itemBabyTvBirthday;
        @BindView(R.id.item_baby_tv_default)
        TextView itemBabyTvDefault;
        @BindView(R.id.item_baby_iv_delete)
        ImageView itemBabyIvDelete;
        @BindView(R.id.item_baby_tv_height)
        TextView itemBabyTvHeight;
        @BindView(R.id.item_baby_tv_weight)
        TextView itemBabyTvWeight;
        public BabyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(MemberBabyEntity.ListBean entity);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }

    public interface OnDeleteClickListener{
        void onDeleteClick(String id);
    }

    public void setOnDeleteClickListener(OnDeleteClickListener listener){
        this.deleteClickListener = listener;
    }

    public interface OnDefaultClickListener{
        void onDefaultClick(String lastId, String currentId);
    }

    public void setOnDefaultClickListener(OnDefaultClickListener listener){
        this.defaultClickListener = listener;
    }
}
